package com.ka.ui;

import com.ka.smartlistview.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import com.ka.ui.KaLayoutParams;

public class SmartListView extends ViewGroup {

	private int hSpace;
	private int vSpace;
	private int itemSelected;
	private SmartListViewStyleAnimation styleAnimation;
	private Boolean isFocus;
	private int numColumn;
	private Boolean fixSize;

	private Context ct;

	// override method
	public SmartListView(Context context) {
		super(context);
	}

	public SmartListView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public SmartListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);

		ct = context;
		TypedArray typeArray = context.obtainStyledAttributes(attrs,
				R.styleable.smart_list_view, 0, 0);

		hSpace = typeArray.getInt(R.styleable.smart_list_view_hspace, hSpace);
		vSpace = typeArray.getInt(R.styleable.smart_list_view_vspace, vSpace);
		styleAnimation = convertAnimationInfo(typeArray
				.getString(R.styleable.smart_list_view_animation_style));

		typeArray.recycle();

		initialize();

	}

	private void initialize() {
		// do some thing
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);

		int rw = MeasureSpec.getSize(widthMeasureSpec);
		// int rh = MeasureSpec.getSize(heightMeasureSpec);

		int h = 0; // current height
		int w = rw; // current width

		int itemWidth = rw / numColumn;

		int childCount = getChildCount();
		int temp_heigh_per_line = 0;

		for (int i = 0; i < childCount; i++) {

			View child = getChildAt(i);
			measureChild(child, widthMeasureSpec, heightMeasureSpec);

			// get height of child
			int child_heigh = child.getMeasuredHeight();

			// find height of line
			temp_heigh_per_line = child_heigh;

			if (i != 0 && i % numColumn == 0) {
				h += temp_heigh_per_line;
			}

			temp_heigh_per_line = temp_heigh_per_line > child_heigh ? temp_heigh_per_line
					: child_heigh;

			// save dimension of view
			KaLayoutParams lp = (KaLayoutParams) child.getLayoutParams();
			lp.x_coord = itemWidth * (i % numColumn);
			lp.y_coord = h;

		}

		h += temp_heigh_per_line;

		setMeasuredDimension(resolveSize(w, widthMeasureSpec),
				resolveSize(h, heightMeasureSpec));

	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		int numOfChildren = this.getChildCount();
		for (int i = 0; i < numOfChildren; i++) {
			View child = this.getChildAt(i);
			KaLayoutParams lp = (KaLayoutParams) child.getLayoutParams();
			child.layout(lp.x_coord, lp.y_coord,
					lp.x_coord + child.getMeasuredWidth(),
					lp.y_coord + child.getMeasuredHeight());
		}

	}

	@Override
	public LayoutParams generateLayoutParams(AttributeSet attrs) {
		return new KaLayoutParams(ct, attrs);
	}

	@Override
	protected LayoutParams generateDefaultLayoutParams() {
		return new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
	}

	@Override
	protected LayoutParams generateLayoutParams(LayoutParams p) {
		return new KaLayoutParams(p);
	}

	@Override
	protected boolean checkLayoutParams(LayoutParams p) {
		return p instanceof KaLayoutParams;
	}

	// method

	public SmartListViewStyleAnimation convertAnimationInfo(String str) {
		if (str.equals("FADE")) {
			return SmartListViewStyleAnimation.FADE;
		} else {
			if (str.equals("FLY")) {
				return SmartListViewStyleAnimation.FLY;
			} else {
				if (str.equals("FLOW")) {
					return SmartListViewStyleAnimation.FLOW;
				}
			}
		}
		return SmartListViewStyleAnimation.NONE;
	}

	// data

	public int gethSpace() {
		return hSpace;
	}

	public void sethSpace(int hSpace) {
		this.hSpace = hSpace;
	}

	public int getvSpace() {
		return vSpace;
	}

	public void setvSpace(int vSpace) {
		this.vSpace = vSpace;
	}

	public int getItemSelected() {
		return itemSelected;
	}

	public void setItemSelected(int itemSelected) {
		this.itemSelected = itemSelected;
	}

	public SmartListViewStyleAnimation getStyleAnimation() {
		return styleAnimation;
	}

	public void setStyleAnimation(SmartListViewStyleAnimation styleAnimation) {
		this.styleAnimation = styleAnimation;
	}

	public static enum SmartListViewStyleAnimation {
		FADE, FLY, FLOW, NONE
	}

}
